package com.zwb.demo.test;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

public class ExampleFactory {
    public static Listener newInstance() throws Exception {
        URLClassLoader tmp =
                new URLClassLoader(new URL[]{getClassPath()}) {
                    public Class loadClass(String name) throws ClassNotFoundException {
                        if ("com.zwb.demo.test.Listener".equals(name))
                            return findClass(name);
                        return super.loadClass(name);
                    }
                };

        return (Listener)
                tmp.loadClass("com.zwb.demo.test.Listener").newInstance();
    }

    private static URL getClassPath() throws MalformedURLException {
        String path = ExampleFactory.class.getResource("").getPath();
        URL url = new URL(path);
        return url;
    }
}